/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LayerListPanel.java
 *
 * Created on 16-mei-2011, 14:47:43
 */
package com.iceberg.adgt.ui.gen;

import com.iceberg.adgt.common.Observer;
import com.iceberg.adgt.common.ObserverInterest;
import com.iceberg.adgt.domain.DomainFacade;
import com.iceberg.adgt.domain.DomainFacadeImpl;
import com.iceberg.adgt.ui.architecture.ArchitectureController;
import com.iceberg.adgt.ui.architecture.LayerListModel;

/**
 * @author Tim Hofman Laptop
 */
public class LayerListPanel extends javax.swing.JPanel implements Observer {

	/**
	 * Creates new form LayerListPanel
	 */
	public LayerListPanel() {
		initComponents();
		init();
	}

	/**
	 * This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		layerList = new javax.swing.JList();

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
		jLabel1.setText("Layers");

		layerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		layerList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
			public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
				layerListValueChanged(evt);
			}
		});
		jScrollPane1.setViewportView(layerList);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(jLabel1)
										.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE))
								.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(20, Short.MAX_VALUE))
		);
	}// </editor-fold>//GEN-END:initComponents

	private void layerListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_layerListValueChanged

		if (!evt.getValueIsAdjusting() && layerList.getSelectedIndex() > -1) {
			this.controller.setSelectedLayer(listModel.getLayerAt(layerList.getSelectedIndex()));
		}
	}//GEN-LAST:event_layerListValueChanged

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JList layerList;
	// End of variables declaration//GEN-END:variables
	private ArchitectureController controller;
	private LayerListModel listModel;

	public final void setController(ArchitectureController controller) {
		this.controller = controller;
	}

	@Override
	public final void notifyChange(ObserverInterest interest) {
		listModel = new LayerListModel(this.controller.getLayers());
		layerList.setModel(listModel);
		layerList.setSelectedIndex(listModel.getIndexOfLayer(this.controller.getSelectedLayer()));
	}

	private void init() {
		DomainFacade facade = DomainFacadeImpl.getInstance();
		facade.addObserver(this, ObserverInterest.LAYERS_CHANGED);
	}
}
